package rsj.service.export.org.test;

import com.rlyy.aibase.utils.base.TableNode;
import common.util.DateTimeFormatterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rsj.api.org.entity.UsageReportParam;
import rsj.service.export.org.IOrgExportCurrentTable;
import rsj.service.usage.IUsageTemplateMethod;
import rsj.sql.org.OrgCurrentDao;

import java.time.LocalDate;
import java.util.Optional;

/**
 * 机构管理-导出当前测试环境表格
 *
 * @author yuanqinglong
 * @since 2021/3/5 15:07
 */
@Component
public class OrgTestCurrentTable implements IOrgExportCurrentTable {

    @Autowired
    private OrgCurrentDao orgCurrentDao;
    /**
     * 导出当前测试环境表格sql
     *
     * @param usageReportParam 参数
     * @return sql
     */
    @Override
    public String generateQueryStatement(UsageReportParam usageReportParam) {
        usageReportParam.setEnvironment(IUsageTemplateMethod.TEST_ENVIRONMENT);
        return IOrgExportCurrentTable.exportCurrentTableQueryStatement(usageReportParam);
    }

    @Override
    public TableNode queryTableByPage(UsageReportParam usageReportParam) {
        return orgCurrentDao.OrgProduceCurrent(usageReportParam);
    }

    /**
     * 生成导出的文件名称
     *
     * @param usageReportParam usageReportParam参数
     * @return 文件名
     */
    @Override
    public String generateExportFileName(UsageReportParam usageReportParam) {
        String deadline = Optional.ofNullable(usageReportParam.getStatDate()).map(DateTimeFormatterUtil::localDateFormatterToStringDate).orElseGet(() -> DateTimeFormatterUtil.localDateFormatterToStringDate(LocalDate.now()));
        return this.queryOrgNameById(usageReportParam.getStatOrgId()) + "-测试环境-截至" + deadline + "-使用情况.xlsx";
    }



    ///**
    // * 提供扩展的默认方法-根据不同业务情况重写此方法二次处理列表数据
    // *
    // * @param tableNode        列表数据
    // * @param connection       数据库连接
    // * @param usageReportParam usageReportParam参数
    // * @return 处理后的列表数据
    // */
    //@Override
    //public String processDataAccordingToTheSituation(TableNode tableNode, Connection connection, UsageReportParam usageReportParam) {
    //    List<CurrentTableColumn> statisticColumnEntities = JSONArray.parseArray(tableNode.toJson(), CurrentTableColumn.class);
    //    if (CollectionUtils.isEmpty(statisticColumnEntities)) {
    //        return tableNode.toJson();
    //    }
    //    // 计算剩余量、查询接口的使用量分组累计
    //    //this.queryInterfaceUsageThroughContract(statisticColumnEntities, connection, IUsageTemplateMethod.TEST_ENVIRONMENT, usageReportParam);
    //    return JSONObject.toJSONString(statisticColumnEntities);
    //}

}
